package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.DeliveryAddress;
import com.ericom.service.GenericManager;

public class DeliveryAddressAction extends BaseAction {
	private GenericManager<DeliveryAddress, Long> deliveryAddressManager;
	private List deliveryAddresss;
	private DeliveryAddress deliveryAddress;
	private Long id;

	public DeliveryAddress getDeliveryAddress() {
		return deliveryAddress;
	}

	public void setDeliveryAddress(DeliveryAddress deliveryAddress) {
		this.deliveryAddress = deliveryAddress;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setDeliveryAddressManager(
			GenericManager<DeliveryAddress, Long> deliveryAddressManager) {
		this.deliveryAddressManager = deliveryAddressManager;
	}

	public String list() {
		deliveryAddresss = deliveryAddressManager.getAll();
		return SUCCESS;
	}

	public List getDeliveryAddresss() {
		return deliveryAddresss;
	}

	public String delete() {
		deliveryAddressManager.remove(deliveryAddress.getId());
		this.saveMessage(getText("deliveryAddress.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			deliveryAddress = deliveryAddressManager.get(id);
		} else {
			deliveryAddress = new DeliveryAddress();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (deliveryAddress.getId() == null);
		deliveryAddress = deliveryAddressManager.save(deliveryAddress);

		String key = (isNew) ? "deliveryAddress.added"
				: "deliveryAddress.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
